package com.example.redisdemo.demos;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class RedisDemo {
    public static void test2(Jedis jedis){
        jedis.set("key","1111");
        jedis.set("key1","222");
        jedis.set("key2","333");
        jedis.set("key3","444");
        //判断key是否存在
        Boolean exists = jedis.exists("key");
        System.out.println(exists);
        //删除key
//        Long del = jedis.del("key");
        //判断key是否存在
        exists = jedis.exists("key");
        System.out.println(exists);
        System.out.println("----------------------");
        //删除多个数据
//        Long del1 = jedis.del("key1", "key2", "key3");

        Set<String> keys = jedis.keys("*");
        System.out.println(keys);

    }
public static void test1(Jedis jedis) throws InterruptedException {
jedis.set("key","1111");
jedis.expire("key",10);
Thread.sleep(2000);
    System.out.println(jedis.ttl("key"));
    System.out.println(jedis.type("key"));
    Thread.sleep(9000);
    boolean key = jedis.exists("key");
    System.out.println(key);
    System.out.println("----------------------");
    jedis.lpush("list","1111","2222","3333");
    System.out.println(jedis.type("list"));
}

public static void test3(Jedis jedis){
//        jedis.mset("key1","1111","key2","2222","key3","3333");
//        List<String> mget = jedis.mget("key1", "key2", "key3");
//        System.out.println(mget);

//    jedis.set("key1","abcdefgh");
//    //截取字符串
//    System.out.println(jedis.getrange("key1", 0, 2));
//    //修改字符串
//    jedis.setrange("key1", 2, "12");
//    System.out.println(jedis.get("key1"));

    jedis.set("key1","1111");
    //追加字符串
    jedis.append("key1","2222");
    System.out.println(jedis.get("key1"));
    System.out.println("----------------------");
    jedis.set("key2","100");
    //自增
    jedis.incr("key2");
    System.out.println(jedis.get("key2"));
    //自增指定值
    jedis.incrBy("key2",2);
    System.out.println(jedis.get("key2"));
    //自减
    jedis.decr("key2");
    System.out.println(jedis.get("key2"));
    //自减指定值
    jedis.decrBy("key2",2);
    System.out.println(jedis.get("key2"));

}

public static void test4(Jedis jedis){
        jedis.lpush("list","1111","2222","3333");
        //获取列表长度
        System.out.println(jedis.llen("list"));
        //获取列表数据
        List<String> list = jedis.lrange("list", 0, -1);
        System.out.println(list);
        //删除列表数据
        Long lrem = jedis.lrem("list", 1, "2222");
        System.out.println(lrem);
        //删除左元素
        String lpop = jedis.lpop("list");
        System.out.println(lpop);
        //删除右元素
        String rpop = jedis.rpop("list");
        System.out.println(rpop);
        //阻塞删除
    List<String> list1 = jedis.brpop(0, "list");
    System.out.println(list1);
}

public static void test5(Jedis jedis){
        jedis.sadd("set","1111","2222","3333");
        jedis.sadd("set2","1111","2222","4444","5555");
        //获取集合长度
        System.out.println(jedis.scard("set"));
        //获取集合数据
        Set<String> set = jedis.smembers("set");
        System.out.println(set);
        //判断元素是否存在
        Boolean sismember = jedis.sismember("set", "1111");
        System.out.println(sismember);
        //删除元素
        Long srem = jedis.srem("set", "1111");
        System.out.println(srem);
        //并集
        Set<String> union = jedis.sunion("set", "set2");
        System.out.println(union);
        //交集
        Set<String> intersect = jedis.sinter("set", "set2");
        System.out.println(intersect);
        //差集
        Set<String> diff = jedis.sdiff("set", "set2");
        System.out.println(diff);
}

public static void test6(Jedis jedis){
        jedis.hset("hash","key1","1111");
        jedis.hset("hash","key2","2222");
        //获取hash长度
        System.out.println(jedis.hlen("hash"));
        //获取hash数据
        System.out.println(jedis.hgetAll("hash"));
        //获取hash指定key的值
        System.out.println(jedis.hget("hash","key1"));
        //判断hash指定key是否存在
        System.out.println(jedis.hexists("hash","key1"));
        //删除hash指定key
        System.out.println(jedis.hdel("hash","key1"));
        //获取hash所有key
        System.out.println(jedis.hkeys("hash"));
        //获取hash所有value
        System.out.println(jedis.hvals("hash"));
        //设置多个hash值
        jedis.hmset("hash", Map.of("key3","3333","key4","4444"));
        //获取多个hash值
        System.out.println(jedis.hmget("hash","key3","key4"));
}

public static void test7(Jedis jedis){
        jedis.zadd("zset",1,"1111");
        jedis.zadd("zset",2,"2222");
        jedis.zadd("zset",3,"3333");
        //获取有序集合长度
        System.out.println(jedis.zcard("zset"));
        //获取有序集合数据
        System.out.println(jedis.zrange("zset",0,-1));
        //获取有序集合指定分数区间的元素
        System.out.println(jedis.zrangeByScore("zset",0,2));
        //获取有序集合指定分数区间的元素个数
        System.out.println(jedis.zcount("zset",0,2));
        //删除有序集合指定元素
        System.out.println(jedis.zrem("zset","1111"));
        //获取有序集合指定元素的分数
        System.out.println(jedis.zscore("zset","2222"));
        //获取有序集合指定元素的排名
        System.out.println(jedis.zrank("zset","3333"));
        //获取有序集合指定元素的排名
        System.out.println(jedis.zrevrank("zset","3333"));
}






    public static void main(String[] args) throws InterruptedException {
        //连接redis服务器
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        //获取jedis对象
        try(Jedis jedis=jedisPool.getResource()){
test7(jedis);


//            String ping = jedis.ping();
//            System.out.println(ping);

//            //清空数据库
//            jedis.flushAll();
//            //设置数据
//            jedis.set("key","1111");
//            //获取数据
//            String val = jedis.get("key");
//            System.out.println(val);
//            System.out.println("----------------------");
//            //设置数据超时时间
//            jedis.setex("key1",1,"设置超时时间的1111");
//            //获取数据
//            String val1 = jedis.get("key1");
//            System.out.println(val1);
//            Thread.sleep(2000);
//            //2秒后获取数据
//            String val2 = jedis.get("key1");
//            System.out.println(val2);
//            System.out.println("----------------------");
//            SetParams params=new SetParams();
//            //设置超时时间
//            params.ex(1);
//            //不存在才能设置成功
//            params.nx();
//            //存在才能设置成功
//            params.xx();
//            jedis.set("key2","设置超时时间的2222",params);


        }
    }
}
